package com.channel.springcloudserviceprotal.controller;

import com.channel.springcloud.model.APIResult;
import com.channel.springcloud.service.GoodsRemoteClient;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class BookController {
    //产品服务的地址（直连）
    private static final String GOODS_SERVICE_URL = "http://localhost:8000/service/goods"; //直连
    //产品服务的地址(服务中心注册的名字application)
    private static final String GOODS_SERVICE_URL_02 = "http://SPRINGCLOUD-SERVICE-GOODS/service/goods";
    //通过Feign远程调用客户端
    @Autowired
    private GoodsRemoteClient goodsRemoteClient;
    /*
       可以进行http接口的调用
       除此之外还有apache httpClient ,java.net.URL相关类也可以
     */
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/cloud/goods")
    public APIResult getAll(){
        //调用远程的contoller
        ResponseEntity<APIResult> entity = restTemplate.getForEntity(GOODS_SERVICE_URL_02, APIResult.class);
        System.out.println(entity.getStatusCode() );
        System.out.println(entity.getStatusCodeValue());
        System.out.println(entity.getHeaders());
        return entity.getBody();
    }

    @GetMapping("/cloud/goodsFeign")
    public APIResult getAllByFeign(){
        //调用远程的contoller 通过Feign方式调用，声明式远程调用 ，goodsRemoteClient就向Dubbon接口层一样
        return goodsRemoteClient.goods();
    }

    /**
     * Hystrix 默认超时时间是一秒，如果在xml配置中开启了hystrix.command.default.execution.timeout.enable: true
     * 会有两个配置来确定会不会降级一个是 Ribbon.ReadTimeout 的配置，默认是一秒，还有就是Hystrix的超时时间
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallback",
        commandProperties = {
                @HystrixProperty(name = "execution.timout.enable",value = "true"),
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
        },ignoreExceptions = Throwable.class)
    @RequestMapping("/cloud/bookHystrix")
    public APIResult bookHystrix(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //调用远程的contoller 通过Feign方式调用，声明式远程调用 ，goodsRemoteClient就向Dubbon接口层一样
        return goodsRemoteClient.goods();
    }

    public APIResult fallback(){
        return new APIResult(1,null,"服务降级了");
    }
    public APIResult fallbackThrowable(Throwable throwable){
        throwable.printStackTrace();
        throwable.getMessage();
        return new APIResult(1,null,"服务降级了");
    }

    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping("/cloud/goodsHystrix")
    public APIResult goodsHystrix(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //调用远程的contoller
        ResponseEntity<APIResult> entity = restTemplate.getForEntity(GOODS_SERVICE_URL_02, APIResult.class);
        return entity.getBody();
    }

}
